using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Edge.Common.ControlFlow;
using Edge.Common.Utils;
using Edge.Common.Web.DataSourceAdapters;
using Edge.Common.ControlFlow;
using Edge.Common.Utils;
using Edge.Common.Web.DataSourceAdapters;

namespace Edge.Common.Web.Controls.Grid
{
    public class SortingHighlighter
    {
        private GridView grid;

        private string ASC_IMG = "img/sort_up.gif";
        private string DESC_IMG = "img/sort_down.gif";
        private bool sortingHighlitaed = false;
        private string sortExpression = string.Empty;
        private SortDirection sortDirection = SortDirection.Ascending;

        public SortingHighlighter(GridView grid)
        {
            if (grid == null)
                throw new ArgumentException("grid");
            this.grid = grid;

            if (!grid.AllowSorting)
                return;

            grid.DataBound += new EventHandler(this.Grid_OnDataBinding);
            grid.RowCreated += Grid_OnRowCreated;

            object dataSource = grid.DataSource;
            BaseDataSource adaptedDataSource = dataSource as BaseDataSource;
            if (adaptedDataSource != null)
            {
                BaseDataSourceView view = (BaseDataSourceView)adaptedDataSource.GetView(BaseDataSource.DEFAULT);
                view.SelectExecuted += View_OnSelectExecuted;
            }

        }

        private void View_OnSelectExecuted(object sender, BaseDataSourceView.SelectedEventArgs e)
        {
            this.sortExpression = e.SortExpression;
            this.sortDirection = e.SortDirection == SortDirection.Ascending ? SortDirection.Ascending : SortDirection.Descending;
            sortingHighlitaed = false;
        }

        private void Grid_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (!sortingHighlitaed)
            {
                if (e.Row.RowType == DataControlRowType.Header)
                {
                    SetupHeader(e.Row);
                    sortingHighlitaed = true;
                }

            }
        }


        private void Grid_OnDataBinding(object sender, EventArgs e)
        {
            sortingHighlitaed = false;
        }


        protected void SetupHeader(GridViewRow row)
        {
            Assert.Check(row.RowType == DataControlRowType.Header);
            if (StringUtils.IsBlankOrNull(this.sortExpression))
                return;
            for (int i = 0; i < grid.Columns.Count; i++)
            {
                DataControlField column = grid.Columns[i];
                if (column.SortExpression == this.sortExpression)
                {
                    string sortImage = string.Format("&nbsp;<img src=\"{0}\" border=\"0\" style=\"padding-right:5pt\" >",
                                                     this.sortDirection == SortDirection.Ascending ? ASC_IMG : DESC_IMG);
                    //row.Cells[i].BackColor = Color.FromArgb(0, 239, 236, 228);
                    row.Cells[i].Controls.AddAt(0, new LiteralControl(sortImage));
                    row.Cells[i].Wrap = false;
                }
            }
        }

       
        protected string SortExpression
        {
            get
            {
                return this.sortExpression;
            }
        }

        protected SortDirection SortDirection
        {
            get
            {
                return this.sortDirection;
            }
        }
    }
}
